﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace HyperPin_Database_Editor
{
    public partial class HPVisualPinballSettings : HPSettingsPanel
    {
        static string defaultPath = "C:\\Program Files\\VisualPinball\\";

        static string iniShowGames = "Show_Games";
        static string iniPath = "Path";
        static string iniTablePath = "Table_Path";
        static string iniTableVideoPath = "Table_Video_Path";
        static string iniTableImagePath = "Table_Image_Path";
        static string iniBackglassImagePath = "Backglass_Image_Path";
        static string iniExecutable = "Exe";

        public HPVisualPinballSettings()
        {
            InitializeComponent();
        }

        public override void doLoadSettings()
        {
            checkEnable.Checked = readSettingBool(iniShowGames, false);

            // Advanced settings.
            textPath.Text = readSetting(iniPath, defaultPath);
            textExecutable.Text = readSetting(iniExecutable, "VPinball.exe");
            textTablePath.Text = readSetting(iniTablePath, Path.Combine(defaultPath, "Tables\\"));

            // HyperPin Directories
            string hyperPinDirectory = MainForm.theMainForm.hyperPinDirectory;
            textVideoPath.Text = readSetting(iniTableVideoPath, Path.Combine(hyperPinDirectory, "Table Videos\\"));
            textImagePath.Text = readSetting(iniTableImagePath, Path.Combine(hyperPinDirectory, "Table Images\\"));
            textBackglassPath.Text = readSetting(iniBackglassImagePath, Path.Combine(hyperPinDirectory, "Backglass Images\\"));

            // Basci settings
            textCompletePath.Text = Path.Combine(textPath.Text, textExecutable.Text);

            updateOptionalControls();
        }

        public override void doSaveSettings()
        {
            writeSettingBool(iniShowGames, checkEnable.Checked);
            writeSetting(iniExecutable, textExecutable.Text);
            if (textPath.Text[textPath.Text.Length - 1] != '\\')
            {
                textPath.Text += '\\';
            }
            writeSetting(iniPath, textPath.Text);

            // Advanced settings.
            if (textTablePath.Text[textTablePath.Text.Length - 1] != '\\')
            {
                textTablePath.Text += '\\';
            }
            writeSetting(iniTablePath, textTablePath.Text);

            if (textVideoPath.Text[textVideoPath.Text.Length - 1] != '\\')
            {
                textVideoPath.Text += '\\';
            }
            writeSetting(iniTableVideoPath, textVideoPath.Text);

            if (textImagePath.Text[textImagePath.Text.Length - 1] != '\\')
            {
                textImagePath.Text += '\\';
            }
            writeSetting(iniTableImagePath, textImagePath.Text);

            if (textBackglassPath.Text[textBackglassPath.Text.Length - 1] != '\\')
            {
                textBackglassPath.Text += '\\';
            }

            writeSetting(iniBackglassImagePath, textBackglassPath.Text);

        }

        public void updateOptionalControls()
        {
            bool enableBasic = checkEnable.Checked && !checkAdvanced.Checked;
            buttonBrowseExecutable.Enabled = enableBasic;
            textCompletePath.Enabled = enableBasic;

            checkAdvanced.Enabled = checkEnable.Checked;

            bool enableAdvanced = checkEnable.Checked && checkAdvanced.Checked;
            labelPath.Enabled = enableAdvanced;
            textPath.Enabled = enableAdvanced;
            labelExecutableFile.Enabled = enableAdvanced;
            textExecutable.Enabled = enableAdvanced;
            labelTablePath.Enabled = enableAdvanced;
            textTablePath.Enabled = enableAdvanced;
            labelVideoPath.Enabled = enableAdvanced;
            textVideoPath.Enabled = enableAdvanced;
            labelImagePath.Enabled = enableAdvanced;
            textImagePath.Enabled = enableAdvanced;
            labelBackglassPath.Enabled = enableAdvanced;
            textBackglassPath.Enabled = enableAdvanced;
        }

        private void checkEnable_CheckedChanged(object sender, EventArgs e)
        {
            updateOptionalControls();
        }

        private void checkAdvanced_CheckedChanged(object sender, EventArgs e)
        {
            updateOptionalControls();
        }

        private void buttonBrowseVPExe_Click(object sender, EventArgs e)
        {
            openFileDialog.FileName = textExecutable.Text;
            openFileDialog.Filter = "Executable Files (*.exe)|*.exe";
            openFileDialog.FilterIndex = 1;
            DialogResult result = openFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                string vpExe = openFileDialog.FileName;

                textCompletePath.Text = vpExe;
            }
        }

        private void textPath_TextChanged(object sender, EventArgs e)
        {
            if (checkAdvanced.Checked)
            {
                textCompletePath.Text = Path.Combine(textPath.Text, textExecutable.Text);
            }
        }

        private void textExecutable_TextChanged(object sender, EventArgs e)
        {
            if (checkAdvanced.Checked)
            {
                textCompletePath.Text = Path.Combine(textPath.Text, textExecutable.Text);
            }
        }

        private void textVisualPinballComplete_TextChanged(object sender, EventArgs e)
        {
            if (!checkAdvanced.Checked)
            {
                textExecutable.Text = Path.GetFileName(textCompletePath.Text);
                textPath.Text = Path.GetDirectoryName(textCompletePath.Text) + "\\";
                textTablePath.Text = Path.GetDirectoryName(textCompletePath.Text) + "\\Tables\\";
            }
        }
    }
}
